package com.xwnet.basic.service.impl;

import com.xwnet.basic.mapper.BasicMapper;
import com.xwnet.basic.query.BasicQuery;
import com.xwnet.basic.service.IBasicService;
import com.xwnet.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> basicMapper;
    @Override
    @Transactional
    public void add(T t) {
        basicMapper.save(t);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        basicMapper.remove(id);
    }

    @Override
    public void batchDel(List<Long> ids) {
         basicMapper.batchRemove(ids);
    }

    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }



    @Override
    public T getById(Serializable id) {
        return basicMapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return basicMapper.loadAll();
    }

    @Override
    public PageList<T> pagequery(BasicQuery query) {
        long total = basicMapper.loadCount(query);
        if (total<1)
            return new PageList<>();
        List<T> rows = basicMapper.loadData(query);
        return new PageList<>(total,rows);
    }
}
